package com.cli.service;

import com.cli.util.ExcelUtil;
import com.cli.util.FileUtil;
import com.cli.util.OcrUtil;
import com.cli.util.WordUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.ClassUtils;

/**
 * @Author xiongmin
 * @Description
 * @Date 2021/2/2 11:46
 * @Version 1.0
 * @TODO
 **/
@Service
public class OcrServiceImpl implements OcrService {

    @Value("${tess.data}")
    private String tessData;

    @Override
    public String doOCR(String filePath) throws Exception {
        if (FileUtil.isWord(filePath)) {
            return WordUtil.read(filePath);
        }
        if (FileUtil.isExcel(filePath)) {
            return ExcelUtil.read(filePath);
        }
        String result = OcrUtil.doOCR(filePath, this.getTessData());
        return result;
    }

    // 获取tessData目录的路径
    private String getTessData() {
        String resourcesPath = ClassUtils.getDefaultClassLoader().getResource(tessData).getPath();
        System.out.println(resourcesPath + "\n" + resourcesPath.substring(1));
        return resourcesPath.substring(1);
    }
}
